<?php

namespace common\models\project;

use Yii;
use yii\behaviors\BlameableBehavior;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "hxdc_project_album_lb".
 *
 * @property int $id
 * @property int $merchant_id 商户id
 * @property int $album_id 相册id
 * @property string $title 标题
 * @property int $album_type 1户型；2相册
 * @property int $media_type 1图片，2VR，3视频
 * @property string $fileId 腾讯云点播fileId
 * @property string $img_url vr链接
 * @property string $vr_url vr链接
 * @property string $video_url vr链接
 * @property int $sort 权重
 * @property int $created_at 创建时间
 * @property int $updated_at 最后一次更新时间
 * @property int $status 状态[-1:删除;0:禁用;1启用]
 */
class ProjectAlbumLb extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'hxdc_project_album_lb';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['merchant_id', 'album_id', 'album_type', 'media_type', 'sort', 'created_at', 'updated_at', 'status'], 'integer'],
            [['album_id', 'title'], 'required'],
            [['title'], 'string', 'max' => 20],
            [['fileId'], 'string', 'max' => 63],
            [['vr_url','img_url','video_url'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'merchant_id' => '商户 ID',
            'album_id' => '相册 ID',
            'title' => '标题',
            'album_type' => '相册类型',
            'media_type' => '媒体类型',
            'fileId' => '腾讯云点播File ID',
            'img_url' => '图片',
            'vr_url' => 'VR链接',
            'video_url' => '视频',
            'sort' => '排序',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'status' => '状态',
        ];
    }
    /**
     * @param bool $insert
     * @return bool
     * @throws \yii\base\Exception
     */
    public function beforeSave($insert)
    {
        if ($this->isNewRecord) {
            $this->created_at = time();
        }

        return parent::beforeSave($insert);
    }
    /**
     * @return array
     */
    public function behaviors()
    {
        $merchant_id = Yii::$app->services->merchant->getId();
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
            [
                'class' => BlameableBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['merchant_id'],
                ],
                'value' => !empty($merchant_id) ? $merchant_id : 0,
            ]
        ];
    }
}
